package com.ias.amq.config;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.core.JmsMessagingTemplate;

@Configuration
public class AmqConfig {
    @Bean(name = "innerAmqConnectionFactory")
    @Primary
    public ActiveMQConnectionFactory innerAmqConnectionFactory(@Value("${spring.inner.activemq.broker-url}") String brokerUrl,
                                                               @Value("${spring.inner.activemq.user}") String username,
                                                               @Value("${spring.inner.activemq.password}") String password) {

        ActiveMQConnectionFactory innerFactory = new ActiveMQConnectionFactory();
        innerFactory.setBrokerURL(brokerUrl);
        innerFactory.setUserName(username);
        innerFactory.setPassword(password);
        return innerFactory;
    }

    @Bean(name = "outerAmqConnectionFactory")
    @Primary
    public ActiveMQConnectionFactory outerAmqConnectionFactory(@Value("${spring.outer.activemq.broker-url}") String brokerUrl,
                                                               @Value("${spring.outer.activemq.user}") String username,
                                                               @Value("${spring.outer.activemq.password}") String password) {

        ActiveMQConnectionFactory outerFactory = new ActiveMQConnectionFactory();
        outerFactory.setBrokerURL(brokerUrl);
        outerFactory.setUserName(username);
        outerFactory.setPassword(password);
        return outerFactory;
    }

    @Bean(name = "innerActivemqTemplate")
    @Primary
    public JmsMessagingTemplate innerActivemqTemplate(
            @Qualifier(value = "innerAmqConnectionFactory") ActiveMQConnectionFactory connectionFactory) {
        JmsMessagingTemplate template = new JmsMessagingTemplate(connectionFactory);
        return template;
    }

    @Bean(name = "outerActivemqTemplate")
    public JmsMessagingTemplate outerActivemqTemplate(
            @Qualifier("outerAmqConnectionFactory") ActiveMQConnectionFactory connectionFactory) {
        JmsMessagingTemplate template = new JmsMessagingTemplate(connectionFactory);
        return template;
    }

    @Bean(name = "innerAmqMessageListener")
    public JmsListenerContainerFactory innerAmqListener(
            @Qualifier("innerAmqConnectionFactory") ActiveMQConnectionFactory connectionFactory) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        return factory;
    }

    @Bean(name = "outerAmqMessageListener")
    public JmsListenerContainerFactory outerAmqListener(
            @Qualifier("outerAmqConnectionFactory") ActiveMQConnectionFactory connectionFactory) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        return factory;
    }
}